package us.versus.them.b3ge.phyxside;

import flash.display.Graphics;

import flash.display.Sprite;
import flash.filters.BitmapFilterQuality;
import flash.filters.BitmapFilter;
import flash.filters.BlurFilter;
import flash.filters.GlowFilter;

import phx.World;
import phx.Body;
import phx.Shape;
import phx.Circle;
import phx.Polygon;

/**
 *
 * Maybe a per shape style would be nicer...
 *
 *
 * see /usr/lib/haxe/lib/physaxe/1,1/phx/FlashDraw.hx
 *
 */
class Painter {
	public function new() {}
	
	public function paint( sprite:Sprite, world:World ) {
		this.paintBody( sprite, world.staticBody, true );
		for ( body in world.bodies ) {
			this.paintBody( sprite, body, false );
		}
	}

	public function paintBody( sprite:Sprite, body:Body, isStatic:Bool ) {
		for ( shape in body.shapes ) {
			this.paintShape( sprite, shape, isStatic );
		}
	}

	public function paintShape( sprite:Sprite, shape:Shape, isStatic:Bool ) {
		switch( shape.type ) {
			case Shape.CIRCLE:  paintCircle( sprite, shape.circle,  isStatic );
			case Shape.POLYGON: paintPolygon( sprite, shape.polygon, isStatic );
		}
	}

	public function paintCircle( sprite:Sprite, shape:Circle, isStatic:Bool ) {
		var bf = sprite.filters;

		var fo = [
			new GlowFilter(
					0xff00ff
					, 1
					, 132
					, 132
					, 150.0
					, BitmapFilterQuality.HIGH
			)
		];

		//	new Array< BlurFilter >();
		//fo.push( new BlurFilter( 50, 50, BitmapFilterQuality.HIGH ) );
		sprite.filters = fo;

		sprite.graphics.lineStyle( 0x0, 2 );
		sprite.graphics.beginFill( 0xff0000, 1 );
		this.drawCircle( sprite, shape );
		sprite.graphics.endFill();

		sprite.filters = bf;

	}

	public function paintPolygon( sprite:Sprite, shape:Polygon, isStatic:Bool ) {
		sprite.graphics.lineStyle( 0x0, 2 );
		sprite.graphics.beginFill( 0x00ff00, 1 );
		this.drawPolygon( sprite, shape );
		sprite.graphics.endFill();
	}

	public function drawCircle( sprite:Sprite, shape:Circle ) {
		sprite.graphics.drawCircle( shape.tC.x, shape.tC.y, shape.r );
	}

	public function drawPolygon( sprite:Sprite, shape:Polygon ) {
		var v = shape.tVerts;
		sprite.graphics.moveTo( v.x, v.y );
		while( v != null ) {
			sprite.graphics.lineTo(v.x, v.y);
			v = v.next;
		}
		sprite.graphics.lineTo( shape.tVerts.x, shape.tVerts.y );
	}

}
